'use strict';

const Controller = require('egg').Controller;

const list = require('./list.json')
const jwt = require('jsonwebtoken');

class HomeController extends Controller {
  //我的
  async index() {
    const {
      ctx
    } = this;
    ctx.body = {
      code: 200,
      list: list.data.body._resblock_list
    }
  }


  //登录
  async login() {
    const {
      ctx
    } = this;
    // console.log(ctx.request.body, 'sdf');
    const {
      username,
      password
    } = ctx.request.body;
    //判断账号
    const result = await this.app.mysql.get('login', {
      username
    });
    if (result) {
      const data = await this.app.mysql.get('login', {
        username,
        password
      })
      if (data) {
        const token = jwt.sign({
          ...data
        }, this.app.config.keys, {
          expiresIn: '24h'
        })
        ctx.body = ({
          code: 200,
          mes: "账号密码正确，登录成功",
          token,
          data
        })

      } else {
        ctx.body = ({
          code: 401,
          mes: '密码错误'
        })
      }
    } else {
      ctx.body = ({
        code: 401,
        mes: '账号错误'
      })
    }
  }

  //轮播图
  async carouse() {
    const {
      ctx
    } = this;
    const data = await this.app.mysql.select('slideshow');
    ctx.body = data;
  }

  //楼市资讯  观看人数
  async information() {
    const {
      ctx
    } = this;
    const data = await this.app.mysql.query(`select * from information order by pageview DESC`)
    ctx.body = data;
  }

  //经纪人
  async broker() {
    const {
      ctx
    } = this;
    const data = await this.app.mysql.select('broker');
    ctx.body = data;
  }

  //楼盘
  async houseresource() {
    const {
      ctx
    } = this;
    const data = await this.app.mysql.select('sellershouse');
    ctx.body = data;
  }


  //总咨询  时间
  async getsuminformation() {
    const {
      ctx
    } = this;
    const data = await this.app.mysql.query(`select * from information order by time desc`)
    ctx.body = data;
  }

  //咨询详情
  async informationDetail() {
    const {
      ctx
    } = this;
    const {
      id
    } = ctx.request.body;
    const data = await this.app.mysql.get('information', {
      id
    });
    ctx.body = data;
  }


  //找房
    //设置用户请求
    async send_help(){
      const {ctx}=this
      const {name,title,tel,user_id}=ctx.request.body.data
      const result = await this.app.mysql.insert('user_home_list', { 
        title:`${title}`,
        name:`${name}`,
        tel:`${tel}`,
        user_id:`${user_id}`
      });
      if(result.affectedRows){
        ctx.body={
          code:200,
          title:"发布成功"
        }
      }
    }
    //获取经纪人的数据
    async find_home_broker(){
      const {ctx}=this
      const lis = await this.app.mysql.select("broker")
      console.log(list,'list...');
      ctx.body={
        code:200,
        lis
      }
    }
  


  //买房 租房
  //获取买房页面的初始数据
  async buy_home() {
    const {
      ctx
    } = this
    const {
      lie
    } = ctx.query
    const lis = await this.app.mysql.select("hot_home")
    if (lie === '单价由低到高') {
      const hots = lis.sort((a, b) => {
        return a.money - b.money
      })
      ctx.body = {
        code: 200,
        hots
      }
    } else if (lie === '单价由高到低') {
      const hots = lis.sort((a, b) => {
        return b.money - a.money
      })
      ctx.body = {
        code: 200,
        hots
      }
    } else if (lie === '默认排序') {
      ctx.body = {
        code: 200,
        hots: lis
      }
    }
  }

  //获取初始数据二
  async buy_home_one() {
    const {
      ctx
    } = this
    const {
      money
    } = ctx.query
    const lis = await this.app.mysql.select("hot_home")
    switch (money) {
      case "0":
        ctx.body = {
          code: 200,
          hots: lis
        }
        return
      case "1000":
        ctx.body = {
          code: 200,
          hots: lis
        }
        return
      default:
        const hots = lis.filter(item => {
          if (+item.money < +money) {
            return item
          }
        })
        ctx.body = {
          code: 200,
          hots
        }
        return
    }
  }

  //获取初始数据三
  async buy_home_two() {
    const {
      ctx
    } = this
    const {
      home,
      homes
    } = ctx.query
    const lis = await this.app.mysql.select("hot_home")
    if (homes === '不限') {
      if (home === '不限') {
        ctx.body = {
          code: 200,
          hots: lis
        }
      } else if (home === '二手房') {
        const hots = lis.filter(item => {
          if (item.genre === '二手') {
            return item
          }
        })
        ctx.body = {
          code: 200,
          hots
        }
      } else {
        const hots = lis.filter(item => {
          if (item.genre !== '二手') {
            return item
          }
        })
        ctx.body = {
          code: 200,
          hots
        }
      }
    } else {
      const hots = lis.filter(item => {
        if (item.title.indexOf(homes) > -1) {
          return item
        }
      })
      ctx.body = {
        code: 200,
        hots
      }
    }
  }

  //获取初始数据四
  async buy_home_three() {
    const {
      ctx
    } = this
    const {
      one,
      two
    } = ctx.query
    const lis = await this.app.mysql.select("hot_home")
    if (one === "不限") {
      const hots = lis.filter(item => {
        if (item.homes === two) {
          return item
        }
      })
      ctx.body = {
        code: 200,
        hots
      }
    } else {
      const arr = lis.filter(item => {
        if (item.verify === one) {
          return item
        }
      })
      const hots = arr.filter(item => {
        if (item.homes === two) {
          return item
        }
      })
      ctx.body = {
        code: 200,
        hots
      }
    }
  }

  //找房模糊搜索
  async buy_home_four() {
    const {
      ctx
    } = this
    const {
      county
    } = ctx.query
    const lis = await this.app.mysql.select("hot_home")
    const hots = lis.filter(item => {
      if (item.county.includes(county)) {
        return item
      }
    })
    ctx.body = {
      code: 200,
      hots
    }
  }

}

module.exports = HomeController;